var contextPath = $("#contextPath").val();
var dataDIV=$("#dataDIV");
var goodsId = $("#goodsId").val();
var spec=$("#spec").val();
var buyNum=$("#buyNum").val();
var type=$("#type").val();
var addressId=$("#addressId").val();
var shoppingcartTips = $('.shoppingcart__tips');
$(function() {
	dataInit();

function dataInit(){
	dataDIV.empty();
	$.ajax({
		url:contextPath+"/address/addressList",
		type:"post",
		async:true,
		dataType:"json",
		success : function(data, textStatus) {
			var state = data.result.state;
			html='';
			if(state==1){
				html='<div class="floor address_select"><ul>';
				if(data.addressList.length>0){
					$.each(data.addressList,function(i,v){
						if(v.status==1){
							html+='<li class="address_select_item poi address_select_active" addressId='+v.id+'> <div class="address_select"><p class="address_info"><span><span class="address_default--tips dib vt">默认</span>';
						}else{
							html+='<li class="address_select_item poi" addressId='+v.id+'><div class="address_select"><p class="address_info"><span>';
						}
						var countryName ='';
						if(v.countryName ==null){
							countryName ='';
						}else{
							countryName = v.countryName
						}
						html+='<span class="name">'+v.receiverName+'</span><span class="phone">'+v.receiverPhone+'</span></span><span class="address_select_top"><a class="icon_modify_01 dib vt"  onclick="editor('+v.id+');" href="javascript:;"></a><a class="icon_del_01 dib vt" id="'+v.id+'" href="javascript:;"></a></span></p> <p class="address_info"><i class="icon_address_01 dib vm"></i><span class="address_info_txt">'+v.provinceName+''+v.cityName+''+countryName+''+v.receiverAddress+'</span></p></div><div class="tr"><i class="icon_address_active"></i></div></li>';
					});
				}
				html+='</ul></div><div class="ui_cells"><div class="ui_cell login_submit"><div class="ui_cell_bd"><a class="ui_btn ui_btn_primary btn_login_submit db tc"  href="javascript:addR();">新增收货地址</a></div></div></div>';
				dataDIV.append(html);
				goodsIsNull();
				$(".address_select li").each(function(){
					 var id=$(this).attr('addressId');
					 if(addressId==id){
						 $(this).siblings().removeClass("address_select_active");
					     $(this).addClass("address_select_active"); 
				      }
					  });
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			layer.open({
			    content:textStatus,
			    time: 10000
			});
		}
	});		
}
});

function goodsIsNull(){
	if(dataDIV.find(".address_info").length==0){
		shoppingcartTips.css('display','flex');
	}else{
		shoppingcartTips.hide();
	}
}
function addR(){
	$("#addressForm").attr("action",contextPath+"/address/addAddress").submit();
}
function editor(id){
	$("#id").val(id);
	$("#addressForm").attr("action",contextPath+"/address/editor").submit();
}

$(document).on('click','.icon_del_01', function() {
	var $this = $(this);
	layer.open({
	      title: '提示',
	      content: '确认删除吗？',
	      btn: ['确认','取消'],
	      className: 'dizhi-layer',
	      yes: function(index) {
	    	  del($this);
	      },
	      no: function(index) {
	        layer.close(index);
	      }
	     });
	
});
function del(object){
	$.ajax({
		url:contextPath + "/address/deleteAddress",
		type:"post",
		async:true,
		dataType:"json",
		data:{"params['addressID']":object.attr('id')},
		success : function(data, textStatus) {
			var state = data.result.state;
			if(state == null){
				layer.open({
				    content: '删除失败！',
				    style: 'background-color:rgba(0,0,0,.8); color:#fff; border:none;',
				    time: 1,
				    shade: false
				    
				});	
			}else{
				var message = data.result.message;
				layer.open({
				    content: message,
				    style: 'background-color:rgba(0,0,0,.8); color:#fff; border:none;',
				    time: 1,
				    shade: false 
				});	
				if(state == 1){
					object.parents('li').remove();
				}
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			layer.open({
			    content:textStatus,
			    time: 10000
			});
		}
	});
}


$(function(){
	// 选择收货地址
	$('#dataDIV').on('click','.address_select li',function(ev){
		var target = ev.target.nodeName.toLowerCase()
		var addressId = $(this).attr('addressId');
		$("#addressId").val(addressId);
		if(target !=='a'){
			$(this).addClass('address_select_active').siblings().removeClass('address_select_active');
			setTimeout(function(){
				if(type=='confirm_shopCart'){
					$("#addressForm").attr("action",contextPath+"/pay/confirm_shopCart").submit();
				}else{
					$("#addressForm").attr("action",contextPath+"/pay/buy_now").submit();
				}
				
			},500)
		}
	});

})